# Author: Chris Moyer
import urllib2
import marajo

def find_class(module_name, class_name=None):
    """
    Find a class given a string separated by dots
    """
    if class_name:
        module_name = "%s.%s" % (module_name, class_name)
    modules = module_name.split('.')
    path = None
    c = None

    for m in modules[1:]:
        if c:
            c = getattr(c, m)
        else:
            c = getattr(__import__(".".join(modules[0:-1])), m)
    return c

def genPassword(length=10):
    """
    Generate a password consisting of "length" alphanumeric characters
    This pulls from random.org

    @param length: The length for this password
    """
    url = "http://www.random.org/cgi-bin/randstring?num=1&len=%s&digits=on&upperalpha=on&loweralpha=on&unique=on&format=text&rnd=new" % (length)
    headers = {"User-Agent": "%s %s (%s)" % (marajo.config.get("application"), marajo.config.get("version"), marajo.config.get("admin_email"))}
    req = urllib2.Request(url, None, headers)
    hand = urllib2.urlopen(req)
    return hand.read().strip()
